package com.qifenqian.turbot.module.adapter.marketing

import android.view.Gravity
import android.view.Menu
import android.view.View
import android.widget.PopupMenu
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.qifenqian.turbot.R
import com.qifenqian.turbot.module.ui.marketing.ACTION_DISCOUNT_ADD
import com.qifenqian.turbot.module.ui.marketing.ACTION_DISCOUNT_EDIT
import kotlinx.android.synthetic.main.item_discount_dish_edit.view.*

/**
 * @author H
 * @createTime 2019/1/4 08:43
 * @description
 * @see
 * @since
 */
class DiscountDishEditAdapter(data: List<String>, private var mAction: String) :
        BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_discount_dish_edit, data) {


    override fun convert(helper: BaseViewHolder, item: String) {
        val view = helper.itemView
        view.tv_item_discount_dish_edit_id.text = "id"
        view.tv_item_discount_dish_edit_name.text = "name"
        view.tv_item_discount_dish_edit_price_small.text = "price"
        view.tv_item_discount_dish_edit_price_middle.text = "price"
        view.tv_item_discount_dish_edit_price_big.text = "price"
        view.et_item_discount_dish_edit_discount_small.setText("8")
        view.et_item_discount_dish_edit_discount_middle.setText("8")
        view.et_item_discount_dish_edit_discount_big.setText("8")
        view.et_item_discount_dish_edit_discount_small.isEnabled = false
        view.et_item_discount_dish_edit_discount_middle.isEnabled = false
        view.et_item_discount_dish_edit_discount_big.isEnabled = false
        view.setOnLongClickListener {
            showPopupMenu(view, helper.layoutPosition)
            return@setOnLongClickListener true
        }
        //通过对焦点状态的监听,来请求编辑完成的接口
        var mHasFocusSmall: Boolean = false
        var mHasFocusMiddle: Boolean = false
        var mHasFocusBig: Boolean = false
       /* view.setOnFocusChangeListener { v, hasFocus ->
            Lcat.a("DiscountDishEditAdapter", hasFocus)
            if (!hasFocus) {
                view.et_item_discount_dish_edit_discount_small.isEnabled = false
                view.et_item_discount_dish_edit_discount_middle.isEnabled = false
                view.et_item_discount_dish_edit_discount_big.isEnabled = false
            } else {
                view.et_item_discount_dish_edit_discount_small.isEnabled = true
                view.et_item_discount_dish_edit_discount_middle.isEnabled = true
                view.et_item_discount_dish_edit_discount_big.isEnabled = true
            }
        }*/
        /*view.et_item_discount_dish_edit_discount_small.setOnFocusChangeListener { v, hasFocus ->
            mHasFocusSmall = hasFocus
            mHasFocusMiddle = false
            mHasFocusBig = false
            initEditView(view, mHasFocusSmall, mHasFocusMiddle, mHasFocusBig)
        }
        view.et_item_discount_dish_edit_discount_middle.setOnFocusChangeListener { v, hasFocus ->
            mHasFocusSmall = false
            mHasFocusMiddle = hasFocus
            mHasFocusBig = false
            initEditView(view, mHasFocusSmall, mHasFocusMiddle, mHasFocusBig)
        }
        view.et_item_discount_dish_edit_discount_big.setOnFocusChangeListener { v, hasFocus ->
            mHasFocusSmall = false
            mHasFocusMiddle = false
            mHasFocusBig = hasFocus
            initEditView(view, mHasFocusSmall, mHasFocusMiddle, mHasFocusBig)
        }*/
    }

    /*private fun initEditView(view: View, mHasFocusSmall: Boolean, mHasFocusMiddle: Boolean, mHasFocusBig: Boolean) {
        Lcat.a("DiscountDishEditAdapter", mHasFocusSmall, mHasFocusMiddle, mHasFocusBig)
        if (!mHasFocusSmall && !mHasFocusMiddle && !mHasFocusBig) {
            view.et_item_discount_dish_edit_discount_small.isEnabled = false
            view.et_item_discount_dish_edit_discount_middle.isEnabled = false
            view.et_item_discount_dish_edit_discount_big.isEnabled = false
        } else {
            view.et_item_discount_dish_edit_discount_small.isEnabled = true
            view.et_item_discount_dish_edit_discount_middle.isEnabled = true
            view.et_item_discount_dish_edit_discount_big.isEnabled = true
        }
    }*/

    private fun showPopupMenu(view: View, position: Int) {
        val popupMenu = PopupMenu(mContext, view, Gravity.END)
        if (mAction == ACTION_DISCOUNT_ADD) {
            popupMenu.menu?.add(Menu.NONE, 0x1, 0, "删除")

        } else if (mAction == ACTION_DISCOUNT_EDIT) {
            popupMenu.menu?.add(Menu.NONE, 0x0, 0, "编辑")
            popupMenu.menu?.add(Menu.NONE, 0x1, 1, "删除")
        }
        popupMenu.setOnMenuItemClickListener {
            when (it.itemId) {
                0x1 -> {
                    remove(position)
                }
                0x0 -> {
                    view.et_item_discount_dish_edit_discount_small.isEnabled = true
                    view.et_item_discount_dish_edit_discount_middle.isEnabled = true
                    view.et_item_discount_dish_edit_discount_big.isEnabled = true
                    view.et_item_discount_dish_edit_discount_small.setTextColor(mContext.resources.getColor(R.color.red_80fe5444))
                    view.et_item_discount_dish_edit_discount_middle.setTextColor(mContext.resources.getColor(R.color.red_80fe5444))
                    view.et_item_discount_dish_edit_discount_big.setTextColor(mContext.resources.getColor(R.color.red_80fe5444))
                }
            }
            return@setOnMenuItemClickListener true
        }
        popupMenu.show()
    }
}
